﻿#include <windows.h>
#include <stdio.h>

BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM Lparam)
{
	HWND hDefView = FindWindowEx(hwnd, 0, L"SHELLDLL_DefView", 0);
	if (hDefView != 0) {
		HWND hWorkerw = FindWindowEx(0, hwnd, L"WorkerW", 0);
		ShowWindow(hWorkerw, SW_HIDE);

		return FALSE;
	}
	return TRUE;
}

int main(int argc, char* argv[])
{
	// 获取屏幕分辨率
	int screenWidth = GetSystemMetrics(SM_CXSCREEN);
	int screenHeight = GetSystemMetrics(SM_CYSCREEN);

	// 构建 ffplay 参数
	wchar_t lpParameter[256];
	swprintf(lpParameter, 256, L" D:\\Downloads\\Compressed\\VideoWallPaperDemo-main\\1440p.mp4 -noborder -loop 0 -fs -vf scale=%d:%d", screenWidth, screenHeight);
	//swprintf(lpParameter, 256, L" D:\\Downloads\\Compressed\\VideoWallPaperDemo-main\\1440p.mp4 -noborder -loop 0 -fs -vf scale=%d:%d -vcodec hevc_cuvid", screenWidth, screenHeight);

	
	// 定义启动参数
	PROCESS_INFORMATION pi{ 0 };
	STARTUPINFO si{ 0 };
	// 初始化进程和启动信息结构体

	if (CreateProcess(L"D:\\Documents\\ffmpeg-5.0.1-full_build\\bin\\ffplay.exe", (LPWSTR)lpParameter, 0, 0, 0, 0, 0, 0, &si, &pi))
	{
		Sleep(2000);
		// 等待进程启动

		HWND hProgman = FindWindow(L"Progman", 0);
		// 查找Progman窗口句柄
		SendMessageTimeout(hProgman, 0x52C, 0, 0, 0, 100, 0);
		// 向Progman窗口发送消息
		HWND hFfplay = FindWindow(L"SDL_app", 0);
		// 查找ffplay窗口句柄
		SetParent(hFfplay, hProgman);
		//// 获取桌面分辨率
		//RECT desktopRect;
		//GetClientRect(hProgman, &desktopRect);
		//int desktopWidth = desktopRect.right - desktopRect.left;
		//int desktopHeight = desktopRect.bottom - desktopRect.top;

		//// 获取视频窗口的原始大小
		//RECT videoRect;
		//GetClientRect(hFfplay, &videoRect);
		//int videoWidth = videoRect.right - videoRect.left;
		//int videoHeight = videoRect.bottom - videoRect.top;

		//// 计算缩放比例
		//double scaleX = (double)desktopWidth / videoWidth;
		//double scaleY = (double)desktopHeight / videoHeight;
		//double scale = max(scaleX, scaleY); // 使用最大缩放比例以铺满全屏

		//// 计算新的视频窗口大小
		//int newVideoWidth = (int)(videoWidth * scale);
		//int newVideoHeight = (int)(videoHeight * scale);

		//// 计算新的视频窗口位置，使其居中
		//int x = (desktopWidth - newVideoWidth) / 2;
		//int y = (desktopHeight - newVideoHeight) / 2;

		//// 调整ffplay窗口的大小和位置
		SetWindowPos(hFfplay, HWND_TOP, 0, 0, screenWidth, screenHeight, SWP_SHOWWINDOW);

		// 设置ffplay窗口的父窗口为Progman
		EnumWindows(EnumWindowsProc, 0);
		// 枚举所有窗口
	}
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return 0;
	// 返回退出码

}
